<?php

	include_once("parser.php");
	//include("PostContainer.php");
	
	class GenericParser extends Parser {
		
		public $base_path = "";
		public $content_path = "";
		public $content_attr = "";
		public $image_path = "";
		public $image_attr = "";
		public $thumbnail_path = "";
		public $thumbnail_attr = "";
		public $url_path = "";
		public $url_attr = "";
	
		//TODO add parameter for subreddit
		function __construct($name) {
			parent::__construct();
			$this->name = $name;
		}
		
		function scrapePosts() {
			$posts = array();
			try {
				
				//use curl to get html of page
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $this->url);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				$html = curl_exec($ch);      
				curl_close($ch);
				
				//set up domxpath to query html
				$dom = new domDocument;
				@$dom->loadHTML($html);
				$dom->preserveWhiteSpace = true;
				$finder = new DomXPath($dom);
				//$classname = " thing id-t3";
				
				//Use query to find specific div with content
				$table = $finder->query($this->base_path);
				$nodeNum = $table->length;
				echo $nodeNum . " Posts<br>";
				
				for($i=0; $i<$nodeNum; $i++) {
					$node = $table->item($i);
					$post = new Post();
					
					//get rank of post
					$post->rank = $i;
					
					$content_node = $finder->query($this->content_path, $node);
					if($this->content_attr == "")
						$post->content = $content_node->item(0)->nodeValue;
					else
						$post->content = $content_node->item(0)->getAtrribute($this->content_attr);
					echo $post->content." content <br>";
					
					$image_node = $finder->query($this->image_path, $node);
					if($this->image_attr == "")
						$post->image = $image_node->item(0)->nodeValue;
					else
						$post->image = $image_node->item(0)->getAttribute($this->image_attr);
					echo $post->image." image <br>";
					
					$url_node = $finder->query($this->url_path, $node);
					if($this->url_attr == "")
						$post->url = $url_node->item(0)->nodeValue;
					else
						$post->url = $url_node->item(0)->getAttribute($this->url_attr);
					echo $post->url." url <br>";
					
					echo "<br>";
					$post->parent = $this->name;
					array_push($posts, $post);
				}
			}
			catch (Exception $e) {
				
			}
			
			return $posts;
		}
	}


?>